package atguigu.aqs;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class Resource {

	
	private Integer number = 0;
	
	private Lock lock = new Mutex(); // �Զ�����
	
	private Condition condition = lock.newCondition(); // �ȴ��ͻ�������
	
	public void increase() {
		lock.lock();
		try {
			while(number != 0 ) {
				condition.await();
			}
			System.out.println(Thread.currentThread().getName()+"\t"+(number++));
			condition.signalAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	
	public void reduce() {
		lock.lock();
		try {
			while(number == 0 ) {
				condition.await();
			}
			System.out.println(Thread.currentThread().getName()+"\t"+(number--));
			condition.signalAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	
}
